

/*
* Copyright @ 2017 com.iflysse.trains
* crm 下午12:59:29
* All right reserved.
*
*/
	
package com.qf.action;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.qf.dto.CrmClassDto;
import com.qf.dto.CrmCourseDto;
import com.qf.dto.CrmStudentDto;
import com.qf.pojo.CrmClass;
import com.qf.pojo.CrmCourseType;

import com.qf.pojo.PageClass;
import com.qf.service.CrmClassService;
import com.qf.service.CrmCourseTypeService;
import com.qf.service.CrmStudentService;
import com.qf.utils.DateUtil;

/**
* @desc: crm
* @descripe:XXXX
* @author: 周锐
* @createTime: 2017年7月26日 下午12:59:29
* @history:
* @version: v1.0

*/
@Controller
public class CrmClassServlet {
    @Autowired
    @Qualifier("crmClassService")
    private CrmClassService classservice;
    @Autowired
    @Qualifier("crmCourseTypeService")
	private CrmCourseTypeService cts;
    @Autowired
    @Qualifier("crmStudentService")
	private CrmStudentService studentService;
	
	
	
	/**
	* @param studentService the studentService to set
	*/
	
	public void setStudentService(CrmStudentService studentService) {
	
		this.studentService = studentService;
	}
	/**
	* @param cts the cts to set
	*/
	
	public void setCts(CrmCourseTypeService cts) {
	
		this.cts = cts;
	}
	/**
	* @param classservice the classservice to set
	*/
	
	public void setClassservice(CrmClassService classservice) {
	
		this.classservice = classservice;
	}
    @RequestMapping("/addClass.do")
    public ModelAndView addClass(HttpServletRequest request,HttpServletResponse response) throws ParseException{
    	ModelAndView mv=new ModelAndView();
    	String className = request.getParameter("className");
    	String courseTypeId=request.getParameter("courseTypeId");
    	String beginTime_ = request.getParameter("beginTime");
    	String endTime_ = request.getParameter("endTime");
    	String remark=request.getParameter("remark");
    	CrmClass crmclass=new CrmClass();
    	crmclass.setClassName(className);
    	crmclass.setBeginTime(DateUtil.tranfaceLong(beginTime_));
    	crmclass.setEndTime(DateUtil.tranfaceLong(endTime_));
    	crmclass.setRemark(remark);
    	CrmCourseType course=new CrmCourseType();
    	course.setCourseTypeId(Long.valueOf(courseTypeId));
    	crmclass.setCrmCourseType(course);
    	classservice.addClass(crmclass);
    	mv.setViewName("findClassbyPage.do");
		return mv;
}
    @RequestMapping("/findClassbyPage.do")
    public ModelAndView findClassbyPage(HttpServletRequest request,HttpServletResponse response){
    	ModelAndView mv=new ModelAndView();
    	String page_=request.getParameter("page");
    	int page=1;
    	int size=3;
    	if(page_!=null){
    		page=Integer.parseInt(page_);
    	}
    	PageClass pageClass = classservice.findClassbyPage(page, size);
    	mv.addObject("pageClass", pageClass);
    	mv.setViewName("/pages/classesm/listClass.jsp");
    	return mv;
    
    }
    @RequestMapping("/preUpdateClass.do")
    public ModelAndView preUpdateClass(HttpServletRequest request,HttpServletResponse response){
    	ModelAndView mv=new ModelAndView();
    	String classId=request.getParameter("classId");
    	String courseTypeId=request.getParameter("courseTypeId");
    	String className=request.getParameter("className");
    	String beginTime=request.getParameter("beginTime");
    	String endTime=request.getParameter("endTime");
    	CrmClassDto crmClass =new CrmClassDto();
    	crmClass.setClassId(Long.valueOf(classId));
    	crmClass.setCourseTypeId(Long.valueOf(courseTypeId));
    	crmClass.setClassName(className);
    	crmClass.setBeginTime(beginTime);
    	crmClass.setEndTime(endTime);
    	List<CrmCourseDto> allCourse = cts.findbyAllCourse();
    	mv.addObject("crmClass", crmClass);
		mv.addObject("allCourse", allCourse);
		mv.setViewName("/pages/classesm/editClass.jsp");
		return mv;
		
}
     @RequestMapping("/updateClass.do")
    public ModelAndView updateClass(HttpServletRequest request,HttpServletResponse response) throws ParseException{
    	 ModelAndView mv=new ModelAndView();
    	String classId=request.getParameter("classId");
     	String className = request.getParameter("className");
     	String courseTypeId=request.getParameter("courseTypeId");
     	String beginTime_ = request.getParameter("beginTime");
     	String endTime_ = request.getParameter("endTime");
     	String remark=request.getParameter("remark");
     	CrmClass crmclass = classservice.findClassbyId(Long.valueOf(classId));
     	crmclass.setClassName(className);
    	crmclass.setBeginTime(DateUtil.tranfaceLong(beginTime_));
    	crmclass.setEndTime(DateUtil.tranfaceLong(endTime_));
    	crmclass.setRemark(remark);
    	CrmCourseType course=new CrmCourseType();
    	course.setCourseTypeId(Long.valueOf(courseTypeId));
    	crmclass.setCrmCourseType(course);
    	classservice.updateClass(crmclass);
    	mv.setViewName("findClassbyPage.do");
		return mv;
}
     @RequestMapping("/preupload.do")
     public ModelAndView preupload(HttpServletRequest request,HttpServletResponse response) {
    	 ModelAndView mv=new ModelAndView();
    	 String classId=request.getParameter("classId");
    	 CrmClass crmclass = classservice.findClassbyId(Long.valueOf(classId));
    	 mv.addObject("crmclass", crmclass);
    	 mv.setViewName("/pages/classesm/uploadClass.jsp");
		return mv;   
     
}
     @RequestMapping("/upload.do")
     public ModelAndView upload(HttpServletRequest request,HttpServletResponse response,@RequestParam("uploadPath") MultipartFile mf) throws IllegalStateException, IOException {
    	 ModelAndView mv=new ModelAndView();
    	 String classId=request.getParameter("classId");
    	 CrmClass crmClass = classservice.findClassbyId(Long.valueOf(classId));
    	 File dir=new File(request.getServletContext().getRealPath("upload"));
    	 System.out.println(dir);
    	 File file=new File(dir,System.currentTimeMillis()+"_"+mf.getOriginalFilename());
    	 mf.transferTo(file);
    	 String uploadPath=dir.getName()+"/"+file.getName();
    	 String uploadFileName=file.getName();
    	 Long uploadTime=System.currentTimeMillis();
    	 crmClass.setUploadPath(uploadPath);
    	 crmClass.setUploadFileName(uploadFileName);
    	 crmClass.setUploadTime(uploadTime);
    	 classservice.updateClass(crmClass);
    	 mv.setViewName("findClassbyPage.do");
		return mv;
}    
    @RequestMapping("/findClassByCourseId.do")
    public @ResponseBody List<CrmClassDto> findClassByCourseId(HttpServletRequest request,HttpServletResponse response){
    	String courseTypeId=request.getParameter("courseTypeId");
    	List<CrmClassDto> crmClasses = classservice.findClassByCourseId(Long.valueOf(courseTypeId));
		return crmClasses;
    	
    }
    @RequestMapping("/showClass.do")
    public ModelAndView showClass(HttpServletRequest request,HttpServletResponse response) {
    	ModelAndView mv=new ModelAndView();
    	String classId=request.getParameter("classId");
    	CrmClassDto classDto = classservice.findClassDtobyId(Long.valueOf(classId));
    	List<CrmStudentDto> studentDtos = studentService.findStudentDtobyClassId(Long.valueOf(classId));
    	mv.addObject("classDto", classDto);
    	mv.addObject("studentDtos", studentDtos);
    	mv.setViewName("/pages/classesm/showClass.jsp");
		return mv;
    	
    }
}	